﻿package org.FaPe3D
{
	import flash.events.Event;
	import flash.utils.*;
	
	import org.FaPe3D.core.cameras.*;
	import org.FaPe3D.core.events.EventManager;
	import org.FaPe3D.core.languages.*;
	import org.FaPe3D.core.scenes.*;
	import org.FaPe3D.core.settings.*;
	import org.FaPe3D.core.viewports.*;
	
	public class Adapter extends EventManager
	{
		public var id:String;
		public static var currentAddapterID:String;
		public var settingPath:String;
		
		public var settings:Settings;
		public var languages:Languages;
		public var viewports:ViewPorts;
		public var scene:Scenes = new Scenes();
		public var cameras:Cameras = new Cameras();
		public var commonClass:*;
		
		public function Adapter(_id:String, _settingPath:String, _commonClass:* = null)
		{
			id = _id;
			currentAddapterID = id;
			settingPath = _settingPath;
			commonClass = _commonClass;
		}
		
		public static function getAdapter(id:String):Adapter
		{
			var ret:Adapter = null;
			for(var i:uint=0; i<FaPe3D.adapters.length; i++)
			{
				if(FaPe3D.adapters[i].id == id)
				{
					// start load & parsing process
					ret = FaPe3D.adapters[i].adapter;	
				}
			}
			
			return ret;
		}
		
		public static function getCurrentAdapter():Adapter
		{
			return getAdapter(currentAddapterID);
		}
		
		public static function changeDefaultAdapter(id:String):Boolean
		{
			if(getAdapter(id) == null)
			{
				return false;
			}else
			{
				currentAddapterID = id;
				return true;
			}
		}
		
		public function start():void
		{
			// run setting class
			settings = new Settings(id, settingPath);
			
			// setup listeners
			settings.addEventListener(Event.COMPLETE, onSettingLoadAndParsCompelete);
		}
		
		private function onSettingLoadAndParsCompelete(e:Event):void
		{
			// run languages class
			languages = new Languages(id);
			
			// setup listeners
			languages.addEventListener(Event.COMPLETE, onLanguageLoadAndParsCompelete);
		}
		
		private function onLanguageLoadAndParsCompelete(e:Event):void
		{
			var vpPath:String = Adapter.getAdapter(id).settings.setting['path.viewports'];
			vpPath = vpPath.replace('{0}', Adapter.getAdapter(id).settings.setting['property.initialviewport'] );
			//
			viewports = new ViewPorts(id, vpPath);	
			// listeners
			viewports.addEventListener(Event.COMPLETE, onViewportLoadAndParsCompeleted)
		}
		
		
		private function onViewportLoadAndParsCompeleted(e:Event):void
		{
			// dic:
			// viewports.viewport
			dispatchEvent(new Event( EventManager.PARSING_COMPLETE ) );
		}
		
		
		
	
		
//			trace(Adapter.getAdapter('test').languages.defaultLanguage.tehran);
//			trace( Adapter.getAdapter('test2').languages.defaultLanguage.tehran );	
		/*
		// test
			trace( Languages.defaultLanguageName );
			trace( Languages.defaultLanguage.tehran );
			Languages.changeDefaultLanguage = 'fa';
			trace( Languages.defaultLanguageName );
			trace( Languages.defaultLanguage.tehran );
			//
			trace( Languages.languages['fa.tehran'] );
			trace( Languages.languages['tehran.fa'] );
			trace( Languages.languages['ar.tehran'] );
			
			
		private function onSettingLoadAndParsingFinished(e:Event):void
		{
			// string test
			trace(Settings.setting['path.languages']);
			
			// uint test
			trace(Settings.setting['property.delay']  + 5);
			trace(Settings.setting['property'][0].delay);
			
			// array test (labguages list)
			trace(Settings.setting['languages'][0].direction);
			trace(Settings.setting['languages.en'].direction);
			
			// custom propertys
			trace(Settings.setting['custom.username']);
			trace(Settings.setting['custom'][0].username);
		}
		*/
		
		
	}
}